package com.vf.admin.platform.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.platform.entity.Element;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 标签
 */
@Repository
public class ElementDao extends BaseDao<Element> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 保存或者更新
     */
    public void saveOrUpdate(Element element){
        if(element.getId()!=null && element.getId().intValue() > 0){
            this.update(element);
        }else{
            element.setId(this.add(element));
        }
    }
    //详情
    public Element get(Integer id){
        if(StringEx.isNullOrLing(id)){
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_element  where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        sbStr.append(" ORDER BY id asc ");
        String sql = sbStr.toString();
        return this.queryObj(sql,paramList, Element.class);
    }

    //列表查询
    public List<Element> findList(Map<String,Object> paraMap){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_element  where 1=1");
        if(!StringEx.isNullOrLing(paraMap.get("menuid"))){
            sbStr.append(" and menuid = ? ");
            paramList.add(paraMap.get("menuid"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("menuidArray"))){
            sbStr.append(" and menuid in ( ? )");
            paramList.add(paraMap.get("menuidArray"));
        }
        sbStr.append(" ORDER BY id asc ");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList,Element.class);
    }

    //获取单个用户所有的接口url
    public List<Element> findElementList(Integer userid){
        if(userid==null){
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("SELECT be.* FROM bt_element be WHERE be.menuid IN (");
        if(!StringEx.isNullOrLing(userid)){
            sbStr.append(" SELECT m.id FROM bt_menu m  WHERE m.id IN (SELECT rm.menuid FROM bt_role_menu rm WHERE rm.roleid IN  ");
            sbStr.append("(SELECT ur.roleid FROM bt_user_role ur WHERE 1 = 1 AND ur.userid = ? )) ORDER BY id DESC)");
            paramList.add(userid);
        }
        String sql = sbStr.toString();
        return this.queryList(sql,paramList,Element.class);
    }



    /**
     * 删除
     */
    public void del(int id){
        if (id <= 0) {
            return;
        }
        String sql = "delete  from bt_element  where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }
    /**
     * 删除
     */
    public void delByMenuId(int menuid){
        if (menuid <= 0) {
            return;
        }
        String sql = "delete  from bt_element  where menuid = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(menuid);
        this.del(sql, paramList);
    }

}